package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.*;
import com.qf.service.CourseService;
import com.qf.service.SpeakerService;
import com.qf.service.SubjectService;
import com.qf.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * @author 张鑫
 * @date 2020/10/19 21:02
 */
@Controller
@RequestMapping("video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private SpeakerService speakerService;

    @Autowired
    private SubjectService subjectService;


    // 展示视频信息
    @RequestMapping("list")
    public String findAll(Model model, QueryVo queryVo,
                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        model.addAttribute("queryVo", queryVo);

        PageHelper.startPage(pageNum, pageSize);
        List<Video> videoList = videoService.findAll(queryVo);

        PageInfo<Video> pageInfo = new PageInfo<Video>(videoList);
        model.addAttribute("page", pageInfo);

        // 查询所有讲师
        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        // 查询所有课程
        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);
        return "/behind/videoList.jsp";
    }

    // 跳转到修改添加页面
    @RequestMapping("addVideo")
    public String addVideo(Model model) {
        // 查询所有讲师
        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        // 查询所有课程
        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);
        return "/behind/addVideo.jsp";
    }

    // 添加或者修改视频信息
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Video video, Integer id) {
        if (id == null) {
            videoService.addVideo(video);
        } else {
            videoService.modifyVideo(video);
        }

        return "redirect:/video/list";
    }

    // 编辑视频信息
    @RequestMapping("edit")
    public String edit(Integer id, Model model) {
        Video video = videoService.findById(id);
        model.addAttribute("video", video);

        // 查询所有讲师
        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        // 查询所有课程
        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);

        return "/behind/addVideo.jsp";

    }

    // 删除单个视频
    @ResponseBody
    @RequestMapping("videoDel")
    public String videoDel(Integer id) {
        videoService.delVideo(id);
        return "success";
    }

    // 批量删除视频
    @RequestMapping("delBatchVideos")
    public String delBatchVideos(Integer[] ids) {

        for(Integer id:ids) {
            videoService.delVideo(id);
        }
        return "redirect:/video/list";
    }

    @RequestMapping("showVideo")
    public String showVideo(Integer videoId, String subjectName, Model model) {

        Video video = videoService.findByVideoId(videoId);

        model.addAttribute("video", video);
        model.addAttribute("subjectName", subjectName);

        List<Subject> subjectList = subjectService.findAll();
        model.addAttribute("subjectList", subjectList);

        //查
        Integer courseId = video.getCourseId();
        Course course = courseService.findCourseById(courseId);
        model.addAttribute("course", course);

        return "/before/section.jsp";
    }

    // 更新播放次数
    @RequestMapping("updatePlayNum")
    public void updatePlayNum(Integer id, Integer playNum) {

        Video video = videoService.findById(id);
        video.setPlayNum(playNum + 1);
        videoService.update(video);
    }


}
